using NUnit.Framework;
using MyNamespace;
using Evaluant.Uss.ObjectContext;
using System.Collections;

namespace NUnit
{
	[TestFixture]
	public class Migration
	{
		Evaluant.Uss.ObjectContext.ObjectContext pmSource, pmDestination;

		public Migration()
		{
			ObjectService factorySource = new ObjectService("engines.config", "Sql");
			pmSource = factorySource.CreateObjectContext();
			factorySource.AddAssembly("NUnit");

			ObjectService factoryDestination = new ObjectService("engines.config", "Xml");
			pmDestination = factoryDestination.CreateObjectContext();
			factoryDestination.AddAssembly("NUnit");
		}

		[TestFixtureSetUp]
		public void Initialize()
		{
			pmSource.InitializeRepository();

			Person p1 = new Person();
			p1.FirstName = "p1";

			Person p2 = new Person();
			p2.FirstName = "p2";

			p1.Deputy = p2;

			pmSource.BeginTransaction();
            pmSource.Serialize(p1);
			pmSource.CommitTransaction();

			pmSource.Clear();
		}

		[Test]
		public void Migrate()
		{
			IList ps = pmSource.Load(typeof(Person));

			foreach(Person p in ps)
			{
				object o = p.Deputy;
			}

			Assert.AreEqual(2, ps.Count);

			pmDestination.InitializeRepository();
			pmDestination.Import(ps);
			
			pmDestination.BeginTransaction();
			pmDestination.Serialize(ps);
			pmDestination.CommitTransaction();

			pmDestination.Clear();

			Assert.AreEqual(2, pmDestination.Load(typeof(Person)).Count);
			Assert.AreEqual(1, pmDestination.Load(typeof(Person), "[FirstName = 'p1'].Deputy[FirstName = 'p2']").Count);
		}

	}
}
